using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using OpenNETCF.Windows.Forms;
using MobileClient.UIProcessService;

namespace MobileClient
{
	/// <summary>
	/// Summary description for ctlGraphicAnnotation.
	/// </summary>
	public class ctlGraphicAnnotation : Control
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.PictureBox imgGraphic;
        private OpenNETCF.Windows.Forms.Signature sigAnnotation;

		private GraphicAnnotation m_objEntry;
		private Bitmap m_objImage = null;
		private string m_strImagePath;

		public ctlGraphicAnnotation()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			m_objEntry = null;
			m_strImagePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName) + "\\Images";
		}

		public GraphicAnnotation Entry
		{
			get { return m_objEntry; }
			set { m_objEntry = value; }
		}

		public string ImagePath
		{
			get { return m_strImagePath; }
			set { m_strImagePath = value; }
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if( components != null )
					components.Dispose();
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.imgGraphic = new System.Windows.Forms.PictureBox();
            this.sigAnnotation = new Signature();
			// 
			// imgGraphic
			// 
			this.imgGraphic.Location = new System.Drawing.Point(3, 6);
			this.imgGraphic.Size = new System.Drawing.Size(230, 140);
			this.imgGraphic.SizeMode = PictureBoxSizeMode.CenterImage;
			//
			// sigAnnotation
			//
            this.sigAnnotation.Location = new System.Drawing.Point(3, 6);
            this.sigAnnotation.Size = new System.Drawing.Size(230, 140);
            this.sigAnnotation.Visible = true;			
			// 
			// ctlGraphicAnnotation
			// 
            this.Controls.Add(this.sigAnnotation);
			this.Controls.Add(this.imgGraphic);
			this.Size = new System.Drawing.Size(236, 152);

		}
		#endregion

		public void LoadSettings()
		{
			if(m_objEntry != null)
			{
				switch(m_objEntry.DisplayMode)
				{
					case DisplayMode.Normal:
						this.imgGraphic.SizeMode = PictureBoxSizeMode.Normal;
						break;
					case DisplayMode.CenterImage:
						this.imgGraphic.SizeMode = PictureBoxSizeMode.CenterImage;
						break;
					case DisplayMode.StretchImage:
						this.imgGraphic.SizeMode = PictureBoxSizeMode.StretchImage;
						break;
                    default:
                        this.imgGraphic.SizeMode = PictureBoxSizeMode.Normal;
                        break;
				}

				if(!String.IsNullOrEmpty(m_objEntry.ImageBytes))
				{
					string strFileName = m_strImagePath;
					strFileName += "\\";
					strFileName += m_objEntry.Title;
					strFileName += ".img";
					byte[] arrData = System.Convert.FromBase64String(m_objEntry.ImageBytes);
					BinaryWriter bw = new BinaryWriter(File.Open(strFileName, FileMode.Create, FileAccess.Write, FileShare.Write));
					bw.Write(arrData);
					bw.Close();
					m_objImage = new Bitmap(strFileName);
					if(imgGraphic.Image != null)
					{
						Bitmap objOldImage = (Bitmap)imgGraphic.Image;
						objOldImage.Dispose();
					}
					imgGraphic.Image = m_objImage;
				}

                if (!String.IsNullOrEmpty(m_objEntry.AnnotationBytes))
				{
                    sigAnnotation.LoadSignatureEx(Convert.FromBase64String(m_objEntry.AnnotationBytes));
				}
			}
		}

		public void SaveSettings()
		{
			if(m_objEntry != null)
			{
                m_objEntry.AnnotationBytes = Convert.ToBase64String(sigAnnotation.GetSignatureEx());
			}
		}
	}
}
